package app.fugema.shop.main.order.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.OnItemClickInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ActivityRecyclerviewListBinding;
import app.fugema.shop.main.order.adapter.GroupBookingListAdapter;
import app.fugema.shop.main.order.model.GroupOrderResult;
import app.fugema.shop.main.order.model.SimpleGroupOrderBean;
import app.fugema.shop.main.order.vm.GroupBookingVM;

public class GroupBookingListActivity extends BaseTitleActivity<ActivityRecyclerviewListBinding, GroupBookingVM> implements OnGetDataListener
{
    /**
     *  我的拼团列表页面
     * @param context
     * @param
     */
    public static void start(Context context)
    {
        //, int source
        Intent intent = new Intent(context, GroupBookingListActivity.class);
        //intent.putExtra("source", source);
        context.startActivity(intent);
    }

    //状态 0全部  1:开团中 2:参团中 3:拼购成功
    /*
    private enum Item
    {
        match("全部", GroupBookingListFragment.getInstance(-1)),
        home("开团中", GroupBookingListFragment.getInstance(0)),
        myCenter("拼购成功", GroupBookingListFragment.getInstance(1));
        public String name;
        public BaseFragment fragment;
        Item(String name, BaseFragment fragment)
        {
            fragment.setTitle(name);
            this.name = name;
            this.fragment = fragment;
        }
    }
    */

    @Override
    protected int getStatusBarColor() {
        return R.color.redColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_list);
    }

    private GroupBookingListAdapter bookingListAdapter;

    private String userKey;

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("拼团列表",R.color.white);
        //source = getIntent().getIntExtra("source", 0);
        /*
        binding.vpPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager())
        {
            @Override
            public Fragment getItem(int position)
            {
                return Item.values()[position].fragment;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position)
            {
                return Item.values()[position].name;
            }

            @Override
            public int getCount()
            {
                return Item.values().length;
            }
        });
        viewUtils.initSlidingTabLayout(binding.stlTab, binding.vpPager);
        if (source < Item.values().length)
        {
            binding.vpPager.setCurrentItem(source, false);
        }
        */

        bookingListAdapter = new GroupBookingListAdapter();
        binding.rvRecyclerView.setAdapter(bookingListAdapter);
        bookingListAdapter.setOnItemClickInterface(new OnItemClickInterface() {
            @Override
            public boolean onItemClick(View view, int onclickType, int itemType, int position, Object bean) {
                if (bean instanceof SimpleGroupOrderBean)
                {

                }
                return false;
            }
        });
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);

        userKey = LoginUtils.getToken();

        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {
        MutableLiveData<LiveDataWrapper<GroupOrderResult>> liveData = viewModel.getGroupOrders(pn, userKey);
        liveData.observeForever(new BaseViewObserver<LiveDataWrapper<GroupOrderResult>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<GroupOrderResult> data)
            {

//                for (CommentProductResult.RecordsBean record : data.data.getData().getRecords()) {
//                    record.setStatus(3);
//                }
                viewUtils.setDataListRefreshLayout(bookingListAdapter, pn, 5, data.data.getData());
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GroupOrderResult> data)
            {
                return pn == 1 && UIUtils.isEmpty(data.data.getData());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            protected void onComplete() {
                super.onComplete();
                liveData.removeObserver(this);
            }
        });
    }




}
